﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DelonixRegiaHMSys.Core.Interfaces;
using DelonixRegiaHMSys.Core.Models;

namespace DelonixRegiaHMSys.Infrastructure.Data
{
    public class EfCheckOutQuery : ICheckOutQuery
    {
        //public IEnumerable<CheckOut> GetAll()
        //{
        //    var context = new hmsDBEntities();
        //    return context.checkOuts
        //        .Select(cout => new CheckOut()
        //        {
        //            CheckOutID = cout.checkOutID,
        //            CheckInID = cout.checkinID,
        //            UserID = cout.userID,
        //            InvoiceID = cout.invoiceID
        //        });
        //}

        public void AddCheckOut(CheckOut co)
        {
            var context = new hmsDBEntities();
            var newCheckOut = context.checkOuts.Create();
            newCheckOut.checkOutID = co.CheckOutID;
            newCheckOut.checkinID = co.CheckInID;
            newCheckOut.userID = co.UserID;
            newCheckOut.invoiceID = co.InvoiceID;
            context.checkOuts.Add(newCheckOut);
            context.SaveChanges();
        }

        public CheckOut RetrieveCheckOutByID(string checkOutID)
        {
            var context = new hmsDBEntities();
            CheckOut co = new CheckOut();
            var retrieveCheckOut = context.checkOuts.FirstOrDefault(cout => cout.checkOutID == checkOutID);
            co.CheckOutID = retrieveCheckOut.checkOutID;
            co.CheckInID = retrieveCheckOut.checkinID;
            co.UserID = retrieveCheckOut.userID;
            co.InvoiceID = retrieveCheckOut.invoiceID;
            return co;
        }

        public void UpdateCheckOut(CheckOut co)
        {
            var context = new hmsDBEntities();
            var checkOutToUpdate = context.checkOuts.FirstOrDefault(cout => cout.checkOutID == co.CheckOutID);
            checkOutToUpdate.checkinID = co.CheckInID;
            checkOutToUpdate.userID = co.UserID;
            checkOutToUpdate.invoiceID = co.InvoiceID;
            context.SaveChanges();
        }

        public void InsertInvoice(Invoice inv)
        {
            var context = new hmsDBEntities();
            var newInvoice = context.invoices.Create();
            newInvoice.invoiceID = inv.InvoiceID;
            newInvoice.guestID = inv.GuestID;
            newInvoice.userID = inv.UserID;
            newInvoice.checkinID = inv.CheckinID;
            newInvoice.checkoutDateTime = inv.CheckOutDateTime;
            newInvoice.roomNum = inv.RoomNum;
            newInvoice.numOfNightsStayed = inv.NumberOfNightsStayed;
            newInvoice.addCosts = Convert.ToDecimal(inv.AddCosts);
            newInvoice.totalCosts = inv.TotalCosts;
            context.invoices.Add(newInvoice);
            context.SaveChanges();
        }

        public void InsertItem(Item itm)
        {
            var context = new hmsDBEntities();
            var newItem = context.items.Create();
            newItem.itemName = itm.ItemName;
            newItem.Description = itm.Description;
            newItem.Cost = itm.Cost;
            newItem.invoiceID = itm.InvoiceID;
            context.items.Add(newItem);
            context.SaveChanges();
        }

        public IEnumerable<Item> GetItem(string invoiceID)
        {
            var context = new hmsDBEntities();
            var query = from i in context.items
                        where i.invoiceID == invoiceID
                        select new Item()
                        {
                            ItemName = i.itemName,
                            Description = i.Description,
                            Cost = i.Cost
                        };
            return query;
        }
    }
}
